package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.callbacks.WriteSimpleEntityCallback;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

/**
 * User: Hasp
 * Date: 06.05.12
 * Time: 17:27
 */
public class SubmitReportActivity extends Activity {
    private final int eventId;
    protected final Activity callback;
    protected final TextArea reportText;

    public SubmitReportActivity(final Brother brother, final ActivityType type, int eventId, Activity callback) {
        super(brother, type);
        this.eventId = eventId;
        this.callback = callback;
        reportText = new TextArea();
    }

    protected ClickHandler createClickHandler(final TextArea reportText, final StackLayoutPanel workArea) {
        return new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (reportText.getText().trim().length() == 0) {
                    Window.alert("Write Report Text!");
                } else {
                    ChapterSystemService.App.getInstance().submitReport(brother.getId(), eventId, reportText.getText(),
                            new WriteSimpleEntityCallback(workArea, callback));
                }
            }
        };
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();

        final VerticalPanel panel = new VerticalPanel();
        panel.setWidth("100%");
        panel.setHeight("100%");
        panel.setBorderWidth(3);

        final Button submitButton = new Button("Submit");
        submitButton.addClickHandler(createClickHandler(reportText, workArea));

        final Button back = new Button("<< Back");
        back.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                callback.drawActivity(workArea);
            }
        });

        final HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setWidth("100%");
        buttonPanel.add(submitButton);
        buttonPanel.add(back);

        buttonPanel.setCellHorizontalAlignment(back, HasAlignment.ALIGN_LEFT);
        buttonPanel.setCellHorizontalAlignment(submitButton, HasAlignment.ALIGN_LEFT);

        panel.add(reportText);
        panel.add(buttonPanel);
        reportText.setHeight((workArea.getOffsetHeight() * 0.8) + "px");
        reportText.setWidth("100%");

        panel.setCellHeight(buttonPanel, "25px");
        panel.setCellHorizontalAlignment(submitButton, HasAlignment.ALIGN_CENTER);

        workArea.add(panel, new HTML("Submit Report"), 4);
    }
}
